#pragma once
#include "machine.h"
#include "cpu/dead6809/dead6809.h"
#include "cpu/dead63701/dead63701.h"

//special macros to add system1 roms
#define AddROMS1(ff,aa,ss,cc)		\
	AddROM(ff,aa,ss,cc,ROM_TEMP);	\
	DupROM(aa + 0x10000);			\
	DupROM(aa + 0x20000);			\
	DupROM(aa + 0x30000);			\
	DupROM(aa + 0x40000);			\
	DupROM(aa + 0x50000);			\
	DupROM(aa + 0x60000);			\
	DupROM(aa + 0x70000);

#define AddROMS2(ff,aa,ss,cc)		\
	AddROM(ff,aa,ss,cc,ROM_TEMP);	\
	DupROM(aa + 0x20000);			\
	DupROM(aa + 0x40000);			\
	DupROM(aa + 0x60000);

#include "NamcoSystem1MMU.h"

class CSystem1Machine:public CMachine
{
friend CNamcoSystem1MMU;
public:
	CDead6809 *MainCPU;							//main cpu
	CDead6809 *SoundCPU;							//sound cpu
	CDead6809 *SubCPU;							//sub cpu
	CDead63701 *MCU;								//mcu
	CNamcoSystem1MMU *MainMMU;					//memory mapper unit for main cpu
	CNamcoSystem1MMU *SubMMU;					//mmu for sub cpu
protected:
	u8 ROM[0x400000];								//main rom, also shared with sub cpu
	u8 SoundROM[0x2C000];						//sound rom
	u8 MCUROM[0xE0000];							//mcu rom
	u8 CharMaskROM[0x20000];					//character mask rom
	u8 CharROM[0x100000];						//character rom
	u8 SpriteROM[0x100000];						//sprite rom
	u8 MainRAM[0x8000];							//main ram
	u8 SubRAM[0x8000];							//sub ram
	u8 SoundRAM[0x2000];							//sound ram
	u8 MCURAM[0x80];								//mcu ram
	u8 SharedRAM[0x800];							//shared ram
	u8 VideoRAM[0x8000];							//video ram
	u8 SpriteRAM[0x1000];						//sprite ram
	u8 PaletteRAM[0x8000];						//palette ram
	u8 NVRAM[0x800];								//nvram
	u8 PControl[0x20];							//playfield control registers
	u32 Palette[0x8000];							//converted 32 bit palette
	u8 *MCUBankPtr;								//current bank mapped into mcu address space
	u8 *SoundROMPtr;								//current mapped sound rom
	u8 Screen[288 * (244 + 16)];				//screen drawn to
	int Scanline;									//current scanline
	u8 CUS30_WaveRAM[0x400];					//namco cus30 wave ram
	u8 ResetFlag;									//force machine to reset
	u8 Input1,Input2,Input3;
	u8 MCUPatch;
protected:
	u32 WatchdogData;
	u32 WatchdogCounter;
protected:
	void WritePalette(u32 addr,u8 data);
	void MCUPatchWrite(u8 data);
	void WatchdogWrite(u32 n);					//check in with watchdog
	void WatchdogReset();						//reset watchdog
	void WatchdogUpdate();						//perform updates (resets)
public:
	CSystem1Machine();
	~CSystem1Machine();
	int Init();
	void Kill();
	void Reset();
	void Poll();
	void Blit();
	void Line(u32 mainc,u32 soundc,int irq);
	void Frame();
	void SaveState(CState *s);
	void LoadState(CState *s);
	void RenderScreen(u8 *dest,int pitch);
	static u8 ReadMainCPU(void *user,u32 addr);
	static void WriteMainCPU(void *user,u32 addr,u8 data);
	static u8 ReadSoundCPU(void *user,u32 addr);
	static void WriteSoundCPU(void *user,u32 addr,u8 data);
	static u8 ReadSubCPU(void *user,u32 addr);
	static void WriteSubCPU(void *user,u32 addr,u8 data);
	static u8 ReadMCU(void *user,u32 addr);
	static void WriteMCU(void *user,u32 addr,u8 data);
};
